<script lang="ts" defer async setup>
import { useUserInfo } from '@/store/user'
import { reactive, onMounted, ref } from 'vue'
import { login } from '@/api/login'
import { useI18n } from 'vue-i18n'
const hasVisited = ref(uni.getStorageSync('hasVisited'))
const userInfoStore = useUserInfo()
const { t } = useI18n()

const baseData = reactive({
  showPassWord: false,
  current: 0,
  registerForm: {
    email: null,
    password: null,
    phone: null,
  },
})

const changType = (e: any) => {
  baseData.current = e
}
const goReg = () => {
  let queryUrl
  if (uni.getStorageSync('code')) {
    queryUrl = `?code=${uni.getStorageSync('code')}`
  }
  if (uni.getStorageSync('promo_code')) {
    queryUrl = `?code=${uni.getStorageSync('promo_code')}`
  }
  // if (!uni.getStorageSync('code') && !uni.getStorageSync('promo_code')) {
  //   window.top?.postMessage('/pages/login/register', window.location.origin)
  //   return
  // }
  // window.top?.postMessage(`/pages/login/register`, window.location.origin)
  uni.navigateTo({
    url: '/pages/login/register',
  })
}
const forget = () => {
  window.top?.postMessage('/pages/login/forget', window.location.origin)
  uni.navigateTo({
    url: '/pages/login/forget',
  })
}
const bind = async () => {
  const params = {
    email: baseData.current ? baseData.registerForm.phone : baseData.registerForm.email,
    password: baseData.registerForm.password,
  }
   if (userInfoStore.isPwa) params.from = 'bookmark'
  const res = await login(params)
  if (res.code == 0) {
    userInfoStore.setUserInfos(res.data)
    userInfoStore.setToken(res.data!.token)
    goHome()
  } else {
    uni.showToast({
      title: res.msg,
      icon: 'none',
    })
  }
  //
  uni.setStorageSync('showTips', 1)
}
const goHome = () => {
  uni.setStorageSync('hasVisited', 'true')
  uni.setStorageSync('url', '/pages/index/index')
  window.top?.postMessage('/pages/index/index', window.location.origin)
  uni.redirectTo({
    url: '/pages/index/index',
  })
}

onMounted(() => {
  // 
  uni.setNavigationBarTitle({
    title: 'Ttask!' + t('Earn money so easy') + '!', // 
  })
})
</script>

<template>
  <view class="login-container">
    <!--  -->
    <view class="bg-decoration">
      <view class="circle circle-1"></view>
      <view class="circle circle-2"></view>
      <view class="circle circle-3"></view>
      <view class="floating-shape shape-1">💰</view>
      <view class="floating-shape shape-2">⭐</view>
      <view class="floating-shape shape-3">🎯</view>
    </view>

    <!--  -->
    <view class="header-section">
      <view class="brand-logo">
        <view class="logo-icon">🎯</view>
        <text class="brand-name">Ttask</text>
      </view>
      <view class="welcome-text">
        <text class="main-title">{{ t('Welcome Back') }}</text>
        <text class="sub-title">{{ t('Sign in to continue earning') }}</text>
      </view>
    </view>

    <!--  -->
    <view class="form-container">
      <!--  -->
      <view class="tab-switcher">
        <view 
          class="tab-item" 
          :class="{ active: baseData.current === 0 }"
          @click="changType(0)"
        >
          <text>📧 Email</text>
        </view>
        <view 
          class="tab-item" 
          :class="{ active: baseData.current === 1 }"
          @click="changType(1)"
        >
          <text>📱 Phone</text>
        </view>
      </view>

      <!--  -->
      <view class="input-group">
        <!--  -->
        <view class="input-wrapper" v-show="baseData.current === 0">
          <view class="input-label">{{ t('Email Address') }}</view>
          <view class="modern-input">
            <input 
              v-model="baseData.registerForm.email"
              type="email"
              :placeholder="t('Enter your email')"
              class="input-field"
            />
            <view class="input-icon">✉️</view>
          </view>
        </view>

        <!--  -->
        <view class="input-wrapper" v-show="baseData.current === 1">
          <view class="input-label">{{ t('Phone Number') }}</view>
          <view class="modern-input">
            <!-- <view class="country-code">+91</view> -->
            <input 
              v-model="baseData.registerForm.phone"
              type="tel"
              :placeholder="t('Enter phone number')"
              class="input-field phone-input"
            />
            <view class="input-icon">📱</view>
          </view>
        </view>

        <!--  -->
        <view class="input-wrapper">
          <view class="input-label">{{ t('Password') }}</view>
          <view class="modern-input">
            <input 
              v-model="baseData.registerForm.password"
              :type="baseData.showPassWord ? 'text' : 'password'"
              :placeholder="t('Enter your password')"
              class="input-field"
            />
            <view 
              class="input-icon clickable" 
              @click="baseData.showPassWord = !baseData.showPassWord"
            >
              {{ baseData.showPassWord ? '🙈' : '👁️' }}
            </view>
          </view>
        </view>
      </view>

      <!--  -->
      <view class="forgot-password-section">
        <!-- <text class="forgot-link" @click="forget()">{{ t('Forgot Password') }}?</text> -->
      </view>

      <!--  -->
      <view class="action-section">
        <button class="login-btn" @click="bind()">
          <text class="btn-text">{{ t('Sign In') }}</text>
          <text class="btn-icon">🚀</text>
        </button>
      </view>

      <!--  -->
      <view class="register-link-section">
        <text class="register-text">{{ t('Don\'t have an account') }}?</text>
        <text class="register-link" @click="goReg()">{{ t('Create Account') }}</text>
      </view>
    </view>


  </view>
</template>

<style lang="scss" scoped>
.login-container {
  min-height: 100vh;
  background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
  position: relative;
  overflow: hidden;
  padding: 40rpx 30rpx;
  box-sizing: border-box;
}

.bg-decoration {
  position: absolute;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  pointer-events: none;

  .circle {
    position: absolute;
    border-radius: 50%;
    background: rgba(255, 255, 255, 0.1);
    backdrop-filter: blur(10px);
  }

  .circle-1 {
    width: 200rpx;
    height: 200rpx;
    top: 15%;
    right: -50rpx;
    animation: float 6s ease-in-out infinite;
  }

  .circle-2 {
    width: 150rpx;
    height: 150rpx;
    bottom: 25%;
    left: -30rpx;
    animation: float 8s ease-in-out infinite reverse;
  }

  .circle-3 {
    width: 100rpx;
    height: 100rpx;
    top: 60%;
    right: 15%;
    animation: float 10s ease-in-out infinite;
  }

  .floating-shape {
    position: absolute;
    font-size: 40rpx;
    animation: bounce 4s ease-in-out infinite;
  }

  .shape-1 {
    top: 25%;
    left: 10%;
    animation-delay: 0s;
  }

  .shape-2 {
    top: 70%;
    right: 20%;
    animation-delay: 1s;
  }

  .shape-3 {
    bottom: 15%;
    left: 70%;
    animation-delay: 2s;
  }
}

@keyframes float {
  0%, 100% { transform: translateY(0px); }
  50% { transform: translateY(-20px); }
}

@keyframes bounce {
  0%, 100% { transform: translateY(0px) rotate(0deg); }
  25% { transform: translateY(-10px) rotate(5deg); }
  75% { transform: translateY(-5px) rotate(-5deg); }
}

.header-section {
  text-align: center;
  margin-bottom: 60rpx;
  z-index: 1;
  position: relative;

  .brand-logo {
    display: flex;
    align-items: center;
    justify-content: center;
    margin-bottom: 40rpx;

    .logo-icon {
      font-size: 60rpx;
      margin-right: 20rpx;
      animation: pulse 2s ease-in-out infinite;
    }

    .brand-name {
      font-size: 48rpx;
      font-weight: bold;
      color: white;
      text-shadow: 0 2px 10px rgba(0, 0, 0, 0.3);
    }
  }

  .welcome-text {
    .main-title {
      display: block;
      font-size: 36rpx;
      font-weight: 600;
      color: white;
      margin-bottom: 10rpx;
    }

    .sub-title {
      display: block;
      font-size: 26rpx;
      color: rgba(255, 255, 255, 0.8);
    }
  }
}

@keyframes pulse {
  0%, 100% { transform: scale(1); }
  50% { transform: scale(1.1); }
}

.form-container {
  background: rgba(255, 255, 255, 0.95);
  backdrop-filter: blur(20px);
  border-radius: 30rpx;
  padding: 50rpx 40rpx;
  box-shadow: 0 20px 40px rgba(0, 0, 0, 0.1);
  position: relative;
  z-index: 1;
  margin-bottom: 40rpx;
}

.tab-switcher {
  display: flex;
  background: #f8f9fa;
  border-radius: 20rpx;
  padding: 8rpx;
  margin-bottom: 40rpx;

  .tab-item {
    flex: 1;
    text-align: center;
    padding: 20rpx;
    border-radius: 16rpx;
    transition: all 0.3s ease;
    cursor: pointer;

    text {
      font-size: 28rpx;
      color: #6c757d;
      font-weight: 500;
    }

    &.active {
      background: linear-gradient(135deg, #667eea, #764ba2);
      box-shadow: 0 4px 15px rgba(102, 126, 234, 0.4);

      text {
        color: white;
      }
    }
  }
}

.input-group {
  .input-wrapper {
    margin-bottom: 30rpx;

    .input-label {
      font-size: 26rpx;
      color: #495057;
      font-weight: 600;
      margin-bottom: 15rpx;
    }

    .modern-input {
      position: relative;
      display: flex;
      align-items: center;
      background: #f8f9fa;
      border: 2px solid #e9ecef;
      border-radius: 16rpx;
      transition: all 0.3s ease;

      &:focus-within {
        border-color: #667eea;
        box-shadow: 0 0 0 6rpx rgba(102, 126, 234, 0.1);
      }

      .country-code {
        padding: 0 20rpx;
        font-size: 28rpx;
        color: #667eea;
        font-weight: 600;
        border-right: 1px solid #e9ecef;
      }

      .input-field {
        flex: 1;
        padding: 25rpx 20rpx;
        border: none;
        background: transparent;
        font-size: 28rpx;
        color: #495057;

        &::placeholder {
          color: #adb5bd;
        }

        &.phone-input {
          padding-left: 15rpx;
        }
      }

      .input-icon {
        padding: 0 20rpx;
        font-size: 32rpx;

        &.clickable {
          cursor: pointer;
          transition: transform 0.2s ease;

          &:active {
            transform: scale(0.9);
          }
        }
      }
    }
  }
}

.forgot-password-section {
  text-align: right;
  margin: 20rpx 0 30rpx;

  .forgot-link {
    font-size: 24rpx;
    color: #667eea;
    font-weight: 500;
    cursor: pointer;
    text-decoration: underline;
    transition: color 0.3s ease;

    &:active {
      color: #764ba2;
    }
  }
}

.action-section {
  margin: 40rpx 0 30rpx;

  .login-btn {
    width: 100%;
    background: linear-gradient(135deg, #667eea, #764ba2);
    border: none;
    border-radius: 20rpx;
    padding: 30rpx;
    display: flex;
    align-items: center;
    justify-content: center;
    box-shadow: 0 8px 25px rgba(102, 126, 234, 0.4);
    transition: all 0.3s ease;

    &:active {
      transform: translateY(2px);
      box-shadow: 0 4px 15px rgba(102, 126, 234, 0.4);
    }

    .btn-text {
      color: white;
      font-size: 32rpx;
      font-weight: 600;
      margin-right: 15rpx;
    }

    .btn-icon {
      font-size: 32rpx;
    }
  }
}

.register-link-section {
  text-align: center;
  padding-top: 20rpx;

  .register-text {
    font-size: 26rpx;
    color: #6c757d;
  }

  .register-link {
    font-size: 26rpx;
    color: #667eea;
    font-weight: 600;
    margin-left: 10rpx;
    cursor: pointer;
    text-decoration: underline;
    transition: color 0.3s ease;

    &:active {
      color: #764ba2;
    }
  }
}

.bottom-decoration {
  position: relative;
  z-index: 1;

  .stats-card {
    background: rgba(255, 255, 255, 0.9);
    backdrop-filter: blur(15px);
    border-radius: 20rpx;
    padding: 30rpx;
    display: flex;
    align-items: center;
    justify-content: space-around;
    box-shadow: 0 10px 30px rgba(0, 0, 0, 0.1);

    .stat-item {
      text-align: center;

      .stat-number {
        display: block;
        font-size: 32rpx;
        font-weight: bold;
        color: #667eea;
        margin-bottom: 8rpx;
      }

      .stat-label {
        display: block;
        font-size: 22rpx;
        color: #6c757d;
      }
    }

    .stat-divider {
      width: 2rpx;
      height: 60rpx;
      background: linear-gradient(to bottom, transparent, #e9ecef, transparent);
    }
  }
}
</style>